﻿using Share;
using System;
using System.Collections.Generic;
using System.Linq;

[Serializable]
public class SmartParam
{
    public string m_strParam = "";

    [NonSerialized]
    public string m_strData = "";

    [NonSerialized]
    float m_fValue;
    public float fValue { get { return m_fValue; } }
    [NonSerialized]
    int m_nValue;
    public int nValue { get { return m_nValue; } }

    [NonSerialized]
    string m_strValue = "";
    public string strValue { get { return m_strValue; } }
    [NonSerialized]
    private List<int> m_intList = new List<int>();
    [NonSerialized]
    private List<long> m_longList = new List<long>();
    [NonSerialized]
    private List<string> m_strList = new List<string>();
    [NonSerialized]
    private float m_fOffset = 0;
    [NonSerialized]
    private bool m_bInited = false;

    public bool IsEmptyString()
    {
        return string.IsNullOrEmpty(m_strValue) || m_strValue == "0";
    }

    public SmartParam(string param)
    {
        m_strParam = param ?? "";
    }

    public SmartParam()
    {
        m_strParam = "";
    }

    public SmartParam(int nValue)
    {
        m_strParam = nValue.ToString();
    }

    public SmartParam(float fValue)
    {
        m_strParam = fValue.ToString();
    }

    #region 获取参数值得接口

    public SmartParam Copy()
    {
        var param = new SmartParam();
        param.m_strParam = m_strParam;
        param.m_strData = m_strData;
        param.m_nValue = m_nValue;
        param.m_fValue = m_fValue;
        param.m_strValue = m_strValue;
        param.m_bInited = true;
        return param;
    }

    static char[] keyOffset = { '|', ',' };
    static string strOffset = "|,";

    public void DoInit(Dictionary<string, string> keyDic)
    {
        try
        {
            m_strList.Clear();
            m_intList.Clear();
            m_longList.Clear();

            if (!string.IsNullOrEmpty(m_strParam) && m_strParam.IndexOf(strOffset) > 0)
            {
                var data = m_strParam.Split(keyOffset, StringSplitOptions.RemoveEmptyEntries);
                if (data.Length > 0)
                {
                    m_strData = data[0];
                    if (!float.TryParse(data.Length > 1 ? data[1] : "0", out m_fOffset))
                        m_fOffset = 0;
                }
                else
                {
                    m_strData = m_strParam;
                }
            }
            else
            {
                m_strData = m_strParam;
            }

            m_fValue = GetValue<float>(keyDic, m_strData) + m_fOffset;
            m_nValue = GetValue<int>(keyDic, m_strData) + (int)m_fOffset;
            m_strValue = GetValue<string>(keyDic, m_strData);

            if (!string.IsNullOrEmpty(m_strValue))
                m_strList = StringTools.GetStringList(m_strValue);

            m_bInited = true;
        }
        catch (Exception ex)
        {
            XKLog.LogError($"初始化SmartParam失败: {ex.Message}");
        }
    }

    private T GetValue<T>(Dictionary<string, string> keyDic, string strParam)
    {
        if (string.IsNullOrEmpty(strParam)) return default(T);

        if (strParam.Contains("#")) // 表数据
        {
            strParam = strParam.Replace("#", "");
            if (keyDic != null && keyDic.TryGetValue(strParam, out var value))
                return ConvertValue<T>(value);
        }
        else if (strParam.Contains("$")) // 公式数据
        {
            strParam = strParam.Replace("$", "");
            // 公式解析逻辑（假设此处需要实现）
        }
        else
        {
            return ConvertValue<T>(strParam);
        }

        return default(T);
    }

    private T ConvertValue<T>(string value)
    {
        try
        {
            return (T)Convert.ChangeType(value, typeof(T));
        }
        catch
        {
            return default(T);
        }
    }

    public List<int> GetIntList()
    {
        if (m_intList.Count == 0 && m_strList.Any())
        {
            foreach (var item in m_strList)
            {
                if (int.TryParse(item, out var intValue))
                    m_intList.Add(intValue);
            }
        }
        return m_intList;
    }

    public List<long> GetLongList()
    {
        if (m_longList.Count == 0 && m_strList.Any())
        {
            foreach (var item in m_strList)
            {
                if (long.TryParse(item, out var longValue))
                    m_longList.Add(longValue);
            }
        }
        return m_longList;
    }

    public List<string> GetStringList()
    {
        if (!m_bInited)
            throw new InvalidOperationException("SmartParam未初始化");

        return m_strList;
    }

    #endregion
}